package com.gukun.springboot.tdengine.service.writeplant;
import com.zaxxer.hikari.HikariDataSource;
public class TDEngineConnPool {
    public synchronized static HikariDataSource getHikariDataSource(String jdbcUrl){
        try{
            HikariDataSource dataSource = new HikariDataSource();
            if(jdbcUrl != null && jdbcUrl.indexOf("TAOS-WS")>-1){
                dataSource.setDriverClassName("com.taosdata.jdbc.ws.WebSocketDriver");
            }else{
                dataSource.setDriverClassName("com.taosdata.jdbc.rs.RestfulDriver");
            }
            dataSource.setJdbcUrl(jdbcUrl);
            dataSource.setConnectionTimeout(600000);
//            dataSource.setMaximumPoolSize(Runtime.getRuntime().availableProcessors());
//            dataSource.setMinimumIdle(Runtime.getRuntime().availableProcessors());

            dataSource.setMaximumPoolSize(30);
            dataSource.setMinimumIdle(1);
            dataSource.setConnectionTestQuery("SELECT SERVER_VERSION()");
            return dataSource;
        }catch (Exception e){
            return null;
        }
    }

    public synchronized static HikariDataSource getHikariDataSource(String jdbcUrl,int poolsize){
        try{
            HikariDataSource dataSource = new HikariDataSource();
            if(jdbcUrl != null && jdbcUrl.indexOf("TAOS-WS")>-1){
                dataSource.setDriverClassName("com.taosdata.jdbc.ws.WebSocketDriver");
            }else{
                dataSource.setDriverClassName("com.taosdata.jdbc.rs.RestfulDriver");
            }
            dataSource.setJdbcUrl(jdbcUrl);
            dataSource.setConnectionTimeout(0);
//            dataSource.setConnectionTimeout(300000);

            dataSource.setMaximumPoolSize(poolsize);
            dataSource.setMinimumIdle(1);
            dataSource.setConnectionTestQuery("SELECT SERVER_VERSION()");
            return dataSource;
        }catch (Exception e){
            return null;
        }
    }
}
